//
//  MZYshowViewController.m
//  WristWatch
//
//  Created by dllo on 15/10/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYshowViewController.h"
#import "MZYHeader.h"
#import "MZYshowCollectionViewCell.h"
#import "MZYshowWebViewController.h"

@interface MZYshowViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, retain)MBProgressHUD *HUD;


@end

@implementation MZYshowViewController

- (void)dealloc {
    [_collectionView release];
    [_arr release];
    [_HUD release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
    [self createData];
}

- (void)createView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 2, HEIGHT / 3);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    [flowLayout release];
    [self.collectionView release];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[MZYshowCollectionViewCell class] forCellWithReuseIdentifier:@"reuser"];
    
    //下拉
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(head)];
//    //上啦
//    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(foot)];
//    
//    self.page = 1;

}

- (void)head {
    [self createData];
}

//- (void)foot {
//    self.page++;
//    NSLog(@"%ld", self.page);
//    
//    NSString *curl = [NSString stringWithFormat:@"http://api.watchnews.app887.com/api/Articles.action?keyword=&npc=0&opc=20&type=%ld&uid=290", self.page];
//    // 解析CollectionView
//    AFHTTPRequestOperationManager *conmanager = [AFHTTPRequestOperationManager manager];
//    [conmanager GET:curl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSMutableArray *yArr = responseObject;
//        for (NSDictionary *tempDic in yArr) {
//            MZYInformationModel *info = [[MZYInformationModel alloc] init];
//            [info setValuesForKeysWithDictionary:tempDic];
//            [self.arr addObject:info];
//            [info release];
//        }
//        
//        if (self.arr.count > 0) {
//            self.HUD.hidden = YES;
//            [self.collectionView reloadData];
//        }
//        [self.collectionView.footer endRefreshing];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    }];

//}
- (void)createData {
    AFHTTPRequestOperationManager *showManager = [AFHTTPRequestOperationManager manager];
    [showManager GET:@"http://api.watchnews.app887.com/api/Articles.action?keyword=&npc=0&opc=20&type=%E5%93%81%E7%89%8C%E8%B5%84%E6%96%99&uid=290" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSArray *mArr = dic[@"root"][@"list"];
        self.arr = [MZYInformationModel MZYBaseModelByArr:mArr];
        
        if (self.arr.count > 0) {
            self.HUD.hidden = YES;
            [self.collectionView reloadData];
        }
        [self.collectionView.header endRefreshing];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MZYshowCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuser" forIndexPath:indexPath];
    MZYInformationModel *info = [[MZYInformationModel alloc] init];
    info = self.arr[indexPath.row];
    cell.showLabel.text = info.title;
    cell.showLabel.textColor = [UIColor whiteColor];
    cell.showLabel.textAlignment = 1;
    [cell.showImageView sd_setImageWithURL:[NSURL URLWithString:info.imglink] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    cell.layer.masksToBounds = YES;
    return cell;

}
#pragma mark web跳转

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"我被点击了11😠");
    MZYshowWebViewController *showVC = [[MZYshowWebViewController alloc] init];
    MZYInformationModel *info = self.arr[indexPath.row];
    showVC.hidesBottomBarWhenPushed = YES;
    showVC.url = info.url;
    [self.navigationController pushViewController:showVC animated:YES];
    [showVC release];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
